//
//  MHTokenListController.m
//  M-Saler
//
//  Created by Teresa on 2020/5/21.
//  Copyright © 2020 Teresa. All rights reserved.
//

#import "MHTokenListController.h"
#import "MHTokenListCell.h"
#import "MHAlertTokenController.h"
#import "MHTokenListRequest.h"
#import "MHTokenModel.h"
#import "MHCreateTokenRequest.h"

@interface MHTokenListController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<MHTokenModel*> *dataList;

@end

@implementation MHTokenListController

-(NSMutableArray<MHTokenModel *> *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"口令列表";
    self.view.backgroundColor = [UIColor whiteColor];
    [MHTokenListCell registerToTableView:self.tableView];
    self.tableView.tableFooterView = [UIView new];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self configNavBarWhite];
    [self fetchData];
}

- (IBAction)createToken:(id)sender {
    
    MHCreateTokenRequest *req = [MHCreateTokenRequest new];
    req.salesman_id = [MHUserModel userModel].theID;
    BLOCKSELF;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        MHAlertTokenController *newVC = [[MHAlertTokenController alloc]initWithNibName:MHAlertTokenController.nameOfClass bundle:nil];
        newVC.view.frame = blockSelf.view.frame;
        [newVC showToken:response.data inController:blockSelf able2Share:YES];
        newVC.copyBlock = ^(NSString * _Nonnull str) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = str;
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"口令已复制" message:@"请自行打开微信分享给好友" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:cancel];
            [blockSelf.navigationController presentViewController:alert animated:YES completion:nil];
        };
        
        MHTokenModel *newModel = [MHTokenModel new];
        newModel.code = response.data;
        [blockSelf.tableView beginUpdates];
        [blockSelf.dataList insertObject:newModel atIndex:0];
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [blockSelf.tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationLeft];
        [blockSelf.tableView endUpdates];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance]tipMessage:response.message];
    }];
    
}

-(void)fetchData{
    MHTokenListRequest *req = [MHTokenListRequest new];
    req.salesman_id = [MHUserModel userModel].theID;
    BLOCKSELF;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        blockSelf.dataList = [response.data mutableCopy];
        [blockSelf.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance]tipMessage:response.message];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MHTokenListCell height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MHTokenListCell *cell = [tableView dequeueReusableCellWithIdentifier:[MHTokenListCell nameOfClass] forIndexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    BLOCKSELF;
    cell.btnClickBlock = ^{
        MHAlertTokenController *newVC = [[MHAlertTokenController alloc]initWithNibName:MHAlertTokenController.nameOfClass bundle:nil];
        newVC.view.frame = blockSelf.view.frame;
        BOOL share = blockSelf.dataList[indexPath.row].status ? NO : YES;
        [newVC showToken:blockSelf.dataList[indexPath.row].code inController:blockSelf able2Share:share];
        
        newVC.copyBlock = ^(NSString * _Nonnull str) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = str;
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"口令已复制" message:@"请自行打开微信分享给好友" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:cancel];
            [blockSelf.navigationController presentViewController:alert animated:YES completion:nil];
        };
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
